The Absolute Beginners Guide To Amos ------------------------------------- Chapter Twenty Three ---------------------- After that bit of fun with SHIFT we need to get back to the nitty gritty commands that you are going to need sooner or later. In this chapter we shall be taking a further look at strings. We have already met the normal string function ($) as well as UPPER$ and LOWER$, but there is a lot more to this subject. Load up Example23.Amos and take a quick look at the code. It is a simple Word Guessing game that makes use of the INSTR command. OK the program isn't great in it's own right but it is a good example of how useful INSTR can be when manipulating strings. Let's take a look at INSTR. Put simply INSTR is used for finding a character or characters inside a string of characters. An example: REM A$ is the source string '-------------------------- A$="I JUST WALKED UP TO THE CHAP AND SAID HELLO" REM B$ is the string to matched '------------------------------ B$="I" REM This is where the check is performed '---------------------------------------- X=INSTR(A$,B$) If A$ does contain B$ (Which in this case it does) then the variable X will hold the position in characters of it's place in A$. So in this case X will return the value 1 because the I is the first letter in A$. If we had searched for a word like UP that would return the position of U (character 15) in A$. If X was to return the value of 0 then this would indicate that no match was found. Taking this example one step further we could add a few lines to the above program like this: IF X=0 THEN PRINT "NO MATCH FOUND" IF X<>0 THEN PRINT "FOUND AT POSITION ";X Initially INSTR appears to be a bit daunting but once you have used it a few times it will be an easy to use and powerful function worth knowing about. INSTR is definitely one for your note book. Now go back to Example23.Amos and study the INSTR parts of it. STRING$ ------- The only other command/function used in Example23.Amos that we haven't looked at yet is the STRING$ function. This is the offending line: Pen 4 : Locate 0,3 : Print String$("-",L) We know all about PEN, LOCATE and PRINT so we won't discuss them. STRING$ allows us to duplicate a string as many times as we want. A simple example could be this. Let us say we want to PRINT a line of dashes (-) across the screen. The screen is in Lowres so it is 40 characters wide. This means we will need 40 of them. We could do this: PRINT"----------------------------------------" Which is ugly and takes up more memory than this: PRINT STRING$("-",40) We are simply telling Amos to duplicate the - 40 times and print it. A lot more professional and compact don't you think? Back to the line in example23.Amos. The only difference there is that we are using the variable L for the amount of duplications. The reason we are using a variable is that the amount of dashes (-) VARY according to the LENgth of the hidden word to be found by the user. We have already covered the LEN function in chapter 16 so take a look back there if you are not sure. We have now covered every command and function used in Example23.Amos but there are a few more string functions I would like to show you while we are on the subject. SPACE$ ------ SPACE$ is a nice place to start. SPACE$ is quite similar to STRING$ in the respect that it can PRINT a given amount of characters. The difference here is that SPACE$ only deals with SPACES. Here is an example use for SPACE$: Let us say you have written a program and you have reserved a line on the screen to print up text messages etc. Now some of the messages are going to be longer than other messages so you will need to erase the line without disturbing anything else on the screen. You could do it like this: LOCATE 0,3: PRINT " " Or you could use this: LOCATE 0,3: PRINT SPACE$(40) Alternatively another good example would be: PRINT "3 SPACES COMING UP";SPACE$(3);"SEE!" I think that speaks for itself. FLIP$ ----- FLIP$ is a function I have never found a use for but I will tell you about it anyway. As you have probably guessed FLIP$ literally FLIPs your string: PRINT FLIP$("FLIP AMOS) Would come out SOMA PILF and that's it really. Following are three more string functions that will be of a lot more use to you in everyday programming. LEFT$, RIGHT$ and MID$ ---------------------- We will first take a look at LEFT$: A$="SOME TEXT" PRINT LEFT$ (A$,4) The above program will PRINT the word SOME on the screen. This is what LEFT$ does it looks at a string starting from the LEFTmost side and reads the specified number of characters (4 in this case) from the source string (in this case A$) If the program read LEFT$(a$,7) it would produce SOME T (remember the space is counted as well) Please load Example23_1.amos for a short demo. RIGHT$ is the same but reads from the rightmost character backward through the string. See also Example23_1.Amos MID$ is very useful, it is a combination of LEFT$ and RIGHT$ really. A$="SOME TEXT" PRINT MID$ (A$,6,4) Would produce TEXT on the screen. The 6 is the character you wish to start with (called the offset) and the 4 is how many characters long you would like it. Example23_1.Amos should make it a bit clearer. OK string functions are pretty heavy going so we will leave it there for now and move on to pastures new. End of Chapter 23 ^^^^^^^^^^^^^^^^^